home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / af_teapots.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  6.1 KB  |  223 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1997. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40.  
  41. /**
  42.  *  fog.c
  43.  *  This program draws 5 red teapots, each at a different 
  44.  *  z distance from the eye, in different types of fog.  
  45.  *  Pressing the left mouse button chooses between 3 types of 
  46.  *  fog:  exponential, exponential squared, and linear.  
  47.  *  In this program, there is a fixed density value, as well 
  48.  *  as fixed start and end values for the linear fog.
  49.  */
  50.  
  51. #include <stdlib.h>
  52. #include <math.h>
  53. #include <GL/glut.h>
  54.  
  55. #include "addfog.h"
  56.  
  57. GLint fogMode;
  58.  
  59. #define TPF_LINEAR 1
  60. #define TPF_EXP 2
  61. #define TPF_EXP2 3
  62.  
  63. int addfog = 0;
  64.  
  65. void
  66. selectFog(int mode)
  67. {
  68.   switch (mode) {
  69.   case GL_LINEAR:
  70.     glFogf(GL_FOG_START, 1.0);
  71.     glFogf(GL_FOG_END, 5.0);
  72.     /* falls through */
  73.   case GL_EXP2:
  74.   case GL_EXP:
  75.     glFogi(GL_FOG_MODE, mode);
  76.     glEnable(GL_FOG);
  77.     addfog = 0;
  78.     glutPostRedisplay();
  79.     break;
  80.   case TPF_LINEAR:
  81.     afFogStartEnd(0.0, 10.0);
  82.     afFogMode(GL_LINEAR);
  83.     glDisable(GL_FOG);
  84.     addfog = 1;
  85.     glutPostRedisplay();
  86.     break;
  87.   case TPF_EXP:
  88.     afFogMode(GL_EXP);
  89.     glDisable(GL_FOG);
  90.     addfog = 1;
  91.     glutPostRedisplay();
  92.     break;
  93.   case TPF_EXP2:
  94.     afFogMode(GL_EXP2);
  95.     glDisable(GL_FOG);
  96.     addfog = 1;
  97.     glutPostRedisplay();
  98.     break;
  99.   case 0:
  100.     exit(0);
  101.   }
  102. }
  103.  
  104. /* Initialize z-buffer, projection matrix, light source,  *  and lighting
  105.    model.  Do not specify a material property here. */
  106. void
  107. myinit(void)
  108. {
  109.   GLfloat position[] =
  110.   {0.0, 3.0, 3.0, 0.0};
  111.   GLfloat local_view[] =
  112.   {0.0};
  113.   GLfloat fogColor[4] =
  114.   {0.5, 0.5, 0.5, 1.0};
  115.  
  116.   glEnable(GL_DEPTH_TEST);
  117.   glEnable(GL_FOG);
  118.   glDepthFunc(GL_LESS);
  119.  
  120.   glLightfv(GL_LIGHT0, GL_POSITION, position);
  121.   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  122.  
  123.   glFrontFace(GL_CW);
  124.   glEnable(GL_LIGHTING);
  125.   glEnable(GL_LIGHT0);
  126.   glEnable(GL_AUTO_NORMAL);
  127.   glEnable(GL_NORMALIZE);
  128.  
  129.   fogMode = GL_EXP;
  130.   glFogi(GL_FOG_MODE, fogMode);
  131.   glFogfv(GL_FOG_COLOR, fogColor);
  132.   afFogColor(0.5, 0.5, 0.5);
  133.   glFogf(GL_FOG_DENSITY, 0.35);
  134.   afFogDensity(0.35);
  135.   glHint(GL_FOG_HINT, GL_DONT_CARE);
  136.   glClearColor(0.5, 0.5, 0.5, 1.0);
  137. }
  138.  
  139. void
  140. renderRedTeapot(GLfloat x, GLfloat y, GLfloat z)
  141. {
  142.   float mat[4];
  143.  
  144.   glPushMatrix();
  145.   glTranslatef(x, y, z);
  146.   mat[0] = 0.1745;
  147.   mat[1] = 0.01175;
  148.   mat[2] = 0.01175;
  149.   mat[3] = 1.0;
  150.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
  151.   mat[0] = 0.61424;
  152.   mat[1] = 0.04136;
  153.   mat[2] = 0.04136;
  154.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
  155.   mat[0] = 0.727811;
  156.   mat[1] = 0.626959;
  157.   mat[2] = 0.626959;
  158.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
  159.   glMaterialf(GL_FRONT, GL_SHININESS, 0.6 * 128.0);
  160.   glutSolidTeapot(1.0);
  161.   glPopMatrix();
  162. }
  163.  
  164. int width, height;
  165.  
  166.  /* display() draws 5 teapots at different z positions. */
  167. void
  168. display(void)
  169. {
  170.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  171.   renderRedTeapot(-4.0, -0.5, -1.0);
  172.   renderRedTeapot(-2.0, -0.5, -2.0);
  173.   renderRedTeapot(0.0, -0.5, -3.0);
  174.   renderRedTeapot(2.0, -0.5, -4.0);
  175.   renderRedTeapot(4.0, -0.5, -5.0);
  176.   if (addfog) {
  177.     afDoFinalFogPass(0, 0, width, height);
  178.   }
  179.   glFlush();
  180. }
  181.  
  182. void
  183. myReshape(int w, int h)
  184. {
  185.   width = w;
  186.   height = h;
  187.   glViewport(0, 0, w, h);
  188.   glMatrixMode(GL_PROJECTION);
  189.   glLoadIdentity();
  190.   if (w <= (h * 3))
  191.     glOrtho(-6.0, 6.0, -2.0 * ((GLfloat) h * 3) / (GLfloat) w,
  192.       2.0 * ((GLfloat) h * 3) / (GLfloat) w, 0.0, 10.0);
  193.   else
  194.     glOrtho(-6.0 * (GLfloat) w / ((GLfloat) h * 3),
  195.       6.0 * (GLfloat) w / ((GLfloat) h * 3), -2.0, 2.0, 0.0, 10.0);
  196.   glMatrixMode(GL_MODELVIEW);
  197.   glLoadIdentity();
  198.   afEyeNearFar(0.0, 10.0);
  199. }
  200.  
  201. int
  202. main(int argc, char **argv)
  203. {
  204.   glutInit(&argc, argv);
  205.   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  206.   glutInitWindowSize(450, 150);
  207.   glutCreateWindow("fogged teapots via post-rendering pass");
  208.   myinit();
  209.   glutReshapeFunc(myReshape);
  210.   glutDisplayFunc(display);
  211.   glutCreateMenu(selectFog);
  212.   glutAddMenuEntry("Fog EXP", GL_EXP);
  213.   glutAddMenuEntry("Fog EXP2", GL_EXP2);
  214.   glutAddMenuEntry("Fog LINEAR", GL_LINEAR);
  215.   glutAddMenuEntry("Two Pass Fog EXP", TPF_EXP);
  216.   glutAddMenuEntry("Two Pass Fog EXP2", TPF_EXP2);
  217.   glutAddMenuEntry("Two Pass Fog LINEAR", TPF_LINEAR);
  218.   glutAddMenuEntry("Quit", 0);
  219.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  220.   glutMainLoop();
  221.   return 0;             /* ANSI C requires main to return int. */
  222. }
  223.